package org.microblog.service.impl;


import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.microblog.dao.BlogDao;
import org.microblog.dao.LikesDao;
import org.microblog.entity.Blog;
import org.microblog.entity.Likes;
import org.microblog.entity.LikesVo;
import org.microblog.service.LikesService;
import org.microblog.util.Page;
import org.microblog.util.SessionFatoryUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class LikesServiceImpl implements LikesService {
    SqlSessionFactory sessionFactory = SessionFatoryUtil.sessionFactory;
    LikesVo vo=new LikesVo();
    @Override
    public int getLikeCountByBid(int bid) throws SQLException {
        SqlSession sqlSession=sessionFactory.openSession();
        LikesDao dao=sqlSession.getMapper(LikesDao.class);
        sqlSession.commit();
        int ret=dao.getLikeCountByBid(bid);
        sqlSession.close();
        return ret;
    }

    @Override
    public int deleteLikesByBid(int bid) throws SQLException {
        SqlSession sqlSession=sessionFactory.openSession();
        LikesDao dao=sqlSession.getMapper(LikesDao.class);
        int ret=dao.deleteLikesByBid(bid);
        sqlSession.commit();
        sqlSession.close();
        return ret;
    }

    @Override
    public void getLikeBlogsByPage(int uid, String keyword, Page pageObj) throws SQLException {
        SqlSession sqlSession=sessionFactory.openSession(true);
        vo.setuId(uid);
        vo.setKeyword(keyword);
        vo.setPageNo(pageObj.getBeginIndex());
        vo.setPageSize(pageObj.getPageSize());

        LikesDao likesDao = sqlSession.getMapper(LikesDao.class);
        int totalCount = likesDao.getLikesCountByUidAndKeyword(vo);
        pageObj.setTotalCount(totalCount);
        if (totalCount > 0) {
            if (pageObj.getCurrPageNo() > pageObj.getTotalPageCount())
                pageObj.setCurrPageNo(pageObj.getTotalPageCount());
            List<Blog> blogList = likesDao.getLikeBlogsByPage(vo);
            pageObj.setBlogsList(blogList);
        } else {
            pageObj.setCurrPageNo(0);
            pageObj.setBlogsList(new ArrayList<>());
        }
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public int addLikes(Likes likes) throws SQLException {
        SqlSession sqlSession=sessionFactory.openSession();
        LikesDao dao=sqlSession.getMapper(LikesDao.class);
        int ret=dao.addLikes(likes);
        sqlSession.commit();
        sqlSession.close();
        return ret;
    }

    @Override
    public Likes findLikes(int uid, int bid) throws SQLException {
        SqlSession sqlSession=sessionFactory.openSession();
        LikesDao dao=sqlSession.getMapper(LikesDao.class);
        sqlSession.commit();
        sqlSession.close();
        Likes likes=dao.findLikes(uid,bid);
        return likes;
    }

    @Override
    public int deleteLikes(Likes likes) throws SQLException {
        SqlSession sqlSession=sessionFactory.openSession();
        LikesDao dao=sqlSession.getMapper(LikesDao.class);
        sqlSession.commit();
        sqlSession.close();
        int ret=dao.deleteLikes(likes);
        return ret;
    }
}
